<?php
/**
 * Created by PhpStorm.
 * User: janderson
 * Date: 12/01/15
 * Time: 09:57
 */

namespace helpers;


trait ExcelHelper {

    public $css = "";
    public $html;
    public $header;
    public $body;
    public $footer;

    public function hpExcelInit(){
        $this->header = "<html><meta http-equiv='Content-Type' content='text/html; charset=Windows-1252'><head><style type='text/css'>@css</style></head>";
        $this->body = "<body>@body</body>";
        $this->footer = "</html>";
    }

    public function hpExcelLoadCss($files){
        $dirCss = ASSETS."css";
        if(is_array($files))
        {
            foreach ($files as $file)
            {
                $this->css .= file_get_contents("$dirCss/$file");
            }
        }
        else
        {
            $this->css = file_get_contents("$dirCss/$files");
        }
        $this->header = str_replace("@css", $this->css, $this->header);
    }

    public function hpExcelLoadBody($body){
        $this->body = str_replace("@body", $body, $this->body);
    }

    public function hpExcelSave(){
        header("Content-type: application/vnd.ms-excel");
        header("Content-Disposition: attachment;Filename=rel.xls");
        echo($this->header.$this->body.$this->footer);
    }

}